package com.wyp.didishige.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;
import com.wyp.didishige.R;
import com.wyp.didishige.utils.ImageLoaderUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 动态图片
 * Created by horry on 2015/4/29.
 */
public class DynamicImagesGridAdapter extends BaseAdapter {
    private List<String> mNetList;//网络图片
    private Context mContext;

    public DynamicImagesGridAdapter(Context context) {
        super();
        this.mContext = context;
        mNetList = new ArrayList<String>();
    }

    public void setmNetList(List<String> mNetList) {
        this.mNetList = mNetList;
        notifyDataSetChanged();
    }

    public List<String> getList(){
        if (mNetList!=null){
            return mNetList;
        }else{
            return new ArrayList<String>();
        }
    }

    @Override
    public int getCount() {
        return mNetList.size();
    }

    @Override
    public Object getItem(int position) {
        return mNetList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();

            convertView = LayoutInflater.from(mContext).inflate(R.layout.dynamic_image_item, null);
            holder.image = (ImageView) convertView.findViewById(R.id.image);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        String url = mNetList.get(position);
        ImageLoaderUtil.ImageLoaderInstance.displayImage(url, holder.image, ImageLoaderUtil.Thumbnail_Image_Options,
                new SimpleImageLoadingListener() {
                    @Override
                    public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                        super.onLoadingComplete(imageUri, view, loadedImage);
                        int x = 0, y = 0;
                        int width = 0, height = 0;
                        Bitmap bitmap = null;
                        int w = loadedImage.getWidth();
                        int h = loadedImage.getHeight();
                        // Matrix m = new Matrix();
                        // float sxy = 0;
                        if (w / h == 4 / 3) {
                            return;
                        }
                        if (w > h) {
                            if (w / h > 3 / 1) {
                                x = (w - 3 * h) / 2;
                                y = 0;
                                width = 3 * h;
                                height = h;
                                // sxy = (float) width / (float)
                                // mLogoDefSize;
                                // m.postScale(sxy, sxy);
                            } else {
                                return;
                            }
                        } else {
                            if (h / w > 3 / 1) {
                                x = 0;
                                y = (h - 3 * w) / 2;
                                width = w;
                                height = 3 * w;
                                // sxy = (float) height / (float)
                                // mLogoDefSize;
                                // m.postScale(sxy, sxy);
                            } else {
                                return;
                            }
                        }
                        bitmap = Bitmap.createBitmap(loadedImage, x, y, width, height);
                        // Bitmap.createBitmap(loadedImage, x, y, width,
                        // height, m, true);
                        ImageView iv = (ImageView) view;
                        if (null == bitmap || bitmap.isRecycled()) {
                            bitmap = loadedImage;
                        }
                        iv.setImageBitmap(bitmap);

                    }
                });
        return convertView;
    }

    class ViewHolder {
        ImageView image;
    }
}